<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 权限模块-用户管理
 */
class AdminGroup extends AdminController {
	public $_validation = null;
	function __construct(){
		parent::__construct();
		$this->load->model('MAdminGroup');
		$this->_validation =  array(
			array('field'=>'name', 'label'=>l('管理员组名称'), 'rules'=>'required|min_length[2]|max_length[255]')
			,array('field'=>'type', 'label'=>l('组类型'), 'rules'=>'required|integer')
			,array('field'=>'status', 'label'=>l('状态'), 'rules'=>'integer')
            ,array('field'=>'remark', 'label'=>l('备注信息'), 'rules'=>'max_length[255]')
            ,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
		);
	}

    /**
     * 管理员组列表
     */
    function index(){
        //$this->output->enable_profiler(TRUE);
        if(!$this->p->read){
            //权限不足
            show_error(l('user_has_nopower'));
        }
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $this->formfilter->addFilter('name', 'like');
        $this->formfilter->addFilter('type', 'where');
        $this->formfilter->addFilter('status', 'where');

        $limit = $this->pagination($this->MAdminGroup->getCount());
        $adminGroup = $this->MAdminGroup->getList($limit);
        $this->assign('adminGroup',$adminGroup);
        $this->layout();
    }

	function add(){
		if(!$this->p->add){
			//权限不足
			show_error(l('user_has_nopower'));
		}
        $obj = $this->MAdminGroup->createVo();
		$obj->status = 1;
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->_validation);
        if($this->form_validation->run()==TRUE){
            $this->load->helper('populate');
            $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
            if($this->MAdminGroup->add($newObj)){
                successAndRedirect(l('add_success'));
            }else{
                errorAndRedirect(l('data_fail'));
            }
        }

        $this->assign('obj', $obj);
        $this->layout();
	}

    function delete($id, $update_time){
        if(!is_numeric($id) || !is_numeric($update_time)){
            show_error(l('id_not_null'));
        }
        if(!$this->MAdminGroup->delete($id, $update_time)){
            errorAndRedirect(l('data_fail'));
        }
        successAndRedirect(l('delete_success'));
    }
	
	function edit($id){
        //$this->output->enable_profiler(TRUE);
		if(!$this->p->edit){
			//权限不足
			show_error(l('user_has_nopower'));
		}
		if(((int)$id) . '' !== $id){
			show_error(l('id_or_updated_not_null'));
		}
		$obj = $this->MAdminGroup->getOne(Array('id'=>$id));
		if(!$obj){
			show_error('此管理员组不存在！');
		}
		$this->load->library('form_validation');
		$this->form_validation->set_rules($this->_validation);
		if($this->form_validation->run()==TRUE){
			$this->load->helper('populate');
			$newObj = populate($obj, $this->form_validation->post());
			if($this->MAdminGroup->update($newObj)){
				successAndRedirect(l('edit_success'));
			}
			//操作冲突
			error(l('data_fail'));
		}
		$this->load->model('MRole');
		$roles = $this->MRole->getAll();
		$this->assign('obj', $obj);
		$this->layout();
	}
}
